package cn.tanghom.baiding.service.system.service;

import cn.tanghom.baiding.api.system.model.Role;
import cn.tanghom.baiding.api.system.service.RoleService;
import cn.tanghom.baiding.core.page.Page;
import cn.tanghom.baiding.core.service.impl.BaseServiceImpl;
import cn.tanghom.baiding.core.utils.CommonUtils;
import cn.tanghom.baiding.service.system.dao.RoleDao;
import cn.tanghom.baiding.service.system.dao.RoleResourcesDao;
import cn.tanghom.baiding.service.system.dao.UserDao;
import cn.tanghom.baiding.service.system.dao.UserRoleDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 角色管理逻辑处理层实现类
 * @author: NEMO
 * @date: 2017-07-10 15:30
 */
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

    @Resource
    private RoleDao roleDao;

    @Resource
    private UserRoleDao userRoleDao;

    @Resource
    private RoleResourcesDao roleResourcesDao;

    @Override
    public String saveOrUpdate(Role role, Long[] userId) {
        try {
            Role checkRoleKey = roleDao.selectByNameOrKey(null, role.getRoleKey());
            Role checkRoleName = roleDao.selectByNameOrKey(role.getName(), null);
            if (role.getId() == null) {// 添加
                if (checkRoleKey != null) {
                    return CommonUtils.msgCallback(false, "key已存在,请换一个!", "", null);
                }
                if (checkRoleName != null) {
                    return CommonUtils.msgCallback(false, "名称已存在,请换一个!", "", null);
                }
                roleDao.insert(role);

            } else {// 修改
                Role r = roleDao.selectByPrimaryKey(role.getId());
                if (checkRoleKey != null && r.getId() != checkRoleKey.getId()) {
                    return CommonUtils.msgCallback(false, "key已存在,请换一个!", "", null);
                }
                if (checkRoleName != null && r.getId() != checkRoleName.getId()) {
                    return CommonUtils.msgCallback(false, "名称已存在,请换一个!", "", null);
                }
                r.setName(role.getName());
                r.setRoleKey(role.getRoleKey());
                r.setDescription(role.getDescription());
                r.setStatus(role.getStatus());
                r.setSort(role.getSort());
                roleDao.update(r);
                userRoleDao.delete(null, r.getId());
            }
            if (null != userId) {
                int userIdSize = userId.length;
                for (int i = 0; i < userIdSize; i++) {
                    userRoleDao.save(role.getId(), userId[i]);
                }
            }
            return CommonUtils.msgCallback(true, "操作成功", "", null);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public Role selectByNameOrKey(String name, String key) {
        return roleDao.selectByNameOrKey(name, key);
    }

    @Override
    public List<Role> selectList() {
        return roleDao.selectListByParams(null, null);
    }

    @Override
    public Page<Role> selectPageByParams(String name, String key, Long index) {
        Page<Role> page = new Page<Role>();
        page.setIndex(index);
        page.setSize(10);
        page.setItems(roleDao.selectPageByParams(name, key, page.getStart(), page.getSize()));
        page.setTotalRecord(roleDao.selectCountByParams(name, key));
        return page;
    }

    @Override
    public List<Map<String, Object>> selectRoleResListByParams(Long roleId, Long resId) {
        return null;
    }

    @Override
    public List<Role> selectUserRoles(Long userId) {
        return roleDao.selectUserRoles(userId);
    }

    @Override
    public void saveRoleRes(Long roleId, Long[] resIds) {
        roleResourcesDao.delete(roleId,null);
        if(null != resIds){
            int resIdsSize = resIds.length;
            for (int i = 0; i < resIdsSize; i++) {
                roleResourcesDao.save(roleId, resIds[i]);
            }
        };
    }

}
